
# $Id: Point.pm 140 2013-04-11 02:35:23Z chengxin.cai $

use strict;

package Circle;
use base 'Shape';

use Switch;
use Carp;
use PerlUtil qw( gen_getter gen_setter );

sub import {
    my $package = shift;
    $package =~ s/.*:://;

    ##export the constructor
    no strict 'refs';
    my ($caller) = caller;
    *{ $caller . '::' . $package } = \&{$package};

    ##install getters and setters
    gen_getter( __PACKAGE__, 'center' );
    gen_getter( __PACKAGE__, 'radius' );

    gen_setter( __PACKAGE__, 'set_center' );
    gen_setter( __PACKAGE__, 'set_radius' );

}

sub Circle {
    my ( $center, $radius ) = @_;

    bless sub {
        my $op = shift;
        switch ($op) {
            case 'center' { return $center; }
            case 'radius' { return $radius; }
            else          { croak 'Invalid operation ' . $op; }
        }
    }, __PACKAGE__;
}

use overload q{""} =>sub {
        my $self = shift;
        my $center = $self->center();

        sprintf "#<%s center: (%.1f, %.1f), radius: %s>",
            ref $self, $center->x(), $center->y(), $self->radius();
};

use constant PI => 3.14;

sub area {
    my $self = shift;

    my $square = sub { my $x = shift; $x * $x };

    PI * &$square( $self->radius() );
}

sub draw {
    my $self = shift;

    print "Draw a circle\n";
}

1;

